package com.fantasy.servlet;

import com.fantasy.entity.Admin;
import com.fantasy.entity.Order;
import com.fantasy.entity.Page;
import com.fantasy.service.OrderService;
import com.fantasy.service.impl.OrderServiceImpl;
import com.fantasy.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/order.do")
public class OrderServlet extends BaseServlet{
    private OrderService orderService=new OrderServiceImpl();
    /**
     * 查询全部方法
     * @param request
     * @param response
     */
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //查询全部
        List<Order> orders = orderService.queryAll();
        //System.out.println();

        //使用gson将list转化为json对象
        Gson gson=new Gson();
        String json = gson.toJson(orders);
        //System.out.println(json);

        //向前端发送json对象
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }


    /**
     * 添加订单
     * @param request
     * @param response
     */
    public void addOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        int money = Integer.parseInt(request.getParameter("money"));
        String orderdate = request.getParameter("orderDate");
        int state =Integer.parseInt( request.getParameter("state"));
        Order order= new Order();
        order.setUserId(userId);
        order.setMoney(money);
        order.setOrderDate(orderdate);
        order.setState(state);
        int insert = orderService.insert(order);
        //System.out.println(insert);
        response.setContentType("text/html;charset=utf-8");
        if (insert==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 修改订单
     * @param request
     * @param response
     */
    public void updateOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        int userId = Integer.parseInt(request.getParameter("userId"));
        int money = Integer.parseInt(request.getParameter("money"));
        String orderdate = request.getParameter("orderDate");
        int state =Integer.parseInt( request.getParameter("state"));
        Order order= new Order();
        order.setId(id);
        order.setUserId(userId);
        order.setMoney(money);
        order.setOrderDate(orderdate);
        order.setState(state);
        int update = orderService.update(order);
        response.setContentType("text/html;charset=utf-8");
        if (update==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 删除order
     * @param request
     * @param response
     * @throws IOException
     */
    public void deleteOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {

        int id = Integer.parseInt(request.getParameter("id"));
        int delete = orderService.delete(id);
        response.setContentType("text/html;charset=utf-8");
        if (delete==1){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void pageQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 0);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.DEFAULT_PAGE_SIZE);

        Page<Order> adminPage = orderService.pageQuery(pageNo, pageSize);

        Gson gson=new Gson();
        String adminJson = gson.toJson(adminPage);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(adminJson);
    }
}
